﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using EShop.BLL;
using EShop.Model;
using EShop.Common;
using System.Threading;

namespace EShop.Client
{
    public partial class frmStockExamine : frmBase
    {
        public frmStockExamine()
        {
            InitializeComponent();
        }

        private int iTotal = 0;//总的记录条数
        private int iPageCount = 0;//总的页数
        private int iCurrentPage = 0;//当前页号
        private int iCurrentRow = 0;//当前记录行
        DataTable dtAllData = new DataTable();

        AsyncMethod.ControlsEndueValueEventHandler OnControlEndueValue = null;

        private void frmStockExamine_Load(object sender, EventArgs e)
        {
            btnFirstPage.Enabled = true;
            btnPrePage.Enabled = true;
            btnEndPage.Enabled = true;
            btnNextPage.Enabled = true;
            lblTotalPage.Enabled = true;
            txtCurrentPage.Enabled = true;
            Thread GetAllDataThread = new Thread(new ThreadStart(GetAllData));
            GetAllDataThread.IsBackground = true;
            GetAllDataThread.Start();
            //GetAllData();
        }

        private void GetAllData()
        {
            dtAllData = new StockInBLL().GetStockExamineList();
            //bindingSource1.DataSource = dtAllData;
            InitPage();
            BindData();
        }

        private void InitPage()
        {
            iTotal = dtAllData.Rows.Count;
            iPageCount = (iTotal / oShopConfigInfo.PageSize);
            if (iTotal % oShopConfigInfo.PageSize != 0) iPageCount = iPageCount + 1;//获取总页数
            OnControlEndueValue = new AsyncMethod.ControlsEndueValueEventHandler(OnToolStripLabelEndureValue);
            this.BeginInvoke(OnControlEndueValue, new object(),
                new AsyncMethod.EndueValueEventArgs(lblTotalPage, "/" + iPageCount.ToString(), OnControlEndueValue));
            //lblTotalPage.Text = "/" + iPageCount.ToString();
        }

        private void BindData()
        {
            int nStartPos = 0;//当前页面的开始记录行
            int nEndPos = 0;//当前页面的结束记录行
            DataTable dtTemp =dtAllData.Clone();
            nStartPos = iCurrentRow;
            if ((iCurrentPage * iMaxPageSize + iMaxPageSize) <= iTotal)
            {
                nEndPos = iCurrentRow + iMaxPageSize;
            }
            else
            {
                nEndPos = iCurrentRow + (iTotal % iMaxPageSize);
            }
            nStartPos = iCurrentRow;
            OnControlEndueValue = new AsyncMethod.ControlsEndueValueEventHandler(OnToolStripTextBoxEndureValueEvent);
            this.BeginInvoke(OnControlEndueValue, new object(),
                new AsyncMethod.EndueValueEventArgs(txtCurrentPage, (iCurrentPage + 1).ToString(), OnControlEndueValue));
            //txtCurrentPage.Text = (iCurrentPage + 1).ToString();
            for (int i = nStartPos; i < nEndPos; i++)
            {
                dtTemp.ImportRow(dtAllData.Rows[i]);
            };
            int iCount = dtTemp.Rows.Count;
            if (iCount <= 0)
                return;
            else
            {
                OnControlEndueValue = new AsyncMethod.ControlsEndueValueEventHandler(OnBindingSourceEndueValueEvent);
                this.BeginInvoke(OnControlEndueValue, new object(), new AsyncMethod.EndueValueEventArgs(bindingSource1, dtTemp, OnControlEndueValue));
                //bindingSource1.DataSource = dtTemp;
            }
        }

        private void btnFirstPage_Click(object sender, EventArgs e)
        {
            iCurrentPage = 0;
            iCurrentRow = iMaxPageSize * iCurrentPage;
            BindData();
        }

        private void btnPrePage_Click(object sender, EventArgs e)
        {
            if (iCurrentPage == 0) return;
            iCurrentPage = iCurrentPage - 1;
            iCurrentRow = iMaxPageSize * iCurrentPage;
            BindData();

        }

        private void btnNextPage_Click(object sender, EventArgs e)
        {
            if ((iCurrentPage + 1) == iPageCount) return;
            iCurrentPage = iCurrentPage + 1;
            iCurrentRow = iMaxPageSize * iCurrentPage;
            BindData();

        }

        private void btnEndPage_Click(object sender, EventArgs e)
        {
            iCurrentPage = iPageCount - 1;
            iCurrentRow = iMaxPageSize * iCurrentPage - 1;
            BindData();

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string strBarcode = SystemLibrary.SQLQuoteName(txtBarcode.Text.Trim());
            if(strBarcode.Length ==0)
            {
                return;
            }
            dtAllData = new StockInBLL().GetStockExamineList(strBarcode);
            InitPage();
            BindData();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtBarcode.Text = "";
            GetAllData();
        }

        private void frmStockExamine_FormClosing(object sender, FormClosingEventArgs e)
        {
            dgvStockList.Dispose();
        }
    }
}
